#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ControlPermissions2Provider : CY.GFive.Core.Providers.DALProvider.IControlPermissions2Provider
	{
		#region Sql
		private static readonly string SqlSelectControlPermissions2 = "SELECT [Id], [ControlKey],[PermissionsKey] FROM [ControlPermissions2] WHERE  [Id]=@Id";

		private static readonly string SqlInsertControlPermissions2 = "INSERT INTO [ControlPermissions2]([ControlKey],[PermissionsKey]) values(@ControlKey,@PermissionsKey);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateControlPermissions2 = "UPDATE [ControlPermissions2] SET [ControlKey] = @ControlKey,[PermissionsKey] = @PermissionsKey WHERE  [Id]=@Id";

		private static readonly string SqlDeleteControlPermissions2 = "DELETE [ControlPermissions2] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllControlPermissions2 = "SELECT [Id], [ControlKey],[PermissionsKey] FROM [ControlPermissions2]";
		#endregion

		#region IControlPermissions2Provider Members
		public Core.Business.ControlPermissions2 Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectControlPermissions2);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ControlPermissions2 controlPermissions2 = new Core.Business.ControlPermissions2();

				if (!reader.IsDBNull(0)) controlPermissions2.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) controlPermissions2.ControlKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) controlPermissions2.PermissionsKey = reader.GetInt32(2);

				return controlPermissions2;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ControlPermissions2 controlPermissions2)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ControlKey", SqlDbType.Int, controlPermissions2.ControlKey);
			sql.AddParameter("@PermissionsKey", SqlDbType.Int, controlPermissions2.PermissionsKey);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertControlPermissions2);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) controlPermissions2.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ControlPermissions2 controlPermissions2)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ControlKey", SqlDbType.Int, controlPermissions2.ControlKey);
			sql.AddParameter("@PermissionsKey", SqlDbType.Int, controlPermissions2.PermissionsKey);
			sql.AddParameter("@Id", SqlDbType.Int, controlPermissions2.Id);
			sql.ExecuteSql(SqlUpdateControlPermissions2);
		}

		public void Delete(Core.Business.ControlPermissions2 controlPermissions2)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, controlPermissions2.Id);
			sql.ExecuteSql(SqlDeleteControlPermissions2);
		}

		public IList<Core.Business.ControlPermissions2> GetAllControlPermissions2()
		{
			IList<Core.Business.ControlPermissions2> controlPermissions2list = new List<Core.Business.ControlPermissions2>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllControlPermissions2);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ControlPermissions2 controlPermissions2 = new Core.Business.ControlPermissions2();

					if (!reader.IsDBNull(0)) controlPermissions2.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) controlPermissions2.ControlKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) controlPermissions2.PermissionsKey = reader.GetInt32(2);

					controlPermissions2.MarkOld();
					controlPermissions2list.Add(controlPermissions2);
				}
				reader.Close();
			}
			return controlPermissions2list;
		}
		#endregion
	}
}
